$(document).ajaxSend(function(e, xhr){
    let token = localStorage.getItem("user_token");
    xhr.setRequestHeader("user_token_header", token);
});
//当前用户
function getUrl(url){
    $.ajax({
        type: "get",
        url: url,
        success:function (result){
            if (result.code === "SUCCESS" && result.data != null){
                let userInfo = result.data;
                $("#id").val(userInfo.id);
                $("#userName").text(userInfo.userName);
                $("#birthday").text("生日:" + userInfo.birthday);
                $("#user-role").text(userInfo.powerStatus);
                $("#message-count").text(userInfo.blessCount);
                downloadUserFile("#user-avatar");
            }else {
                alert(result.errorMessage);
            }
        },error:function (error){
            if (error != null && error.status === 401){
                location.href = "login.html";
            }
        }
    })
}
//寿星列表
function getIsBirthday(url){
    $.ajax({
        type:"get",
        url:url,
        success:function (result){
            if (result.code === "SUCCESS" && result.data != null){
                let userInfoList = result.data;
                let finalHtml = '';
                finalHtml += '<h3 class="birthday-star-title">🎉 今日寿星 🎉</h3>';
                for (let userInfo of userInfoList){
                    finalHtml += '<div class="birthday-star-container">';
                    finalHtml += '<img id="' + userInfo.id + '" src="" alt="寿星头像" class="birthday-star-avatar">';
                    finalHtml += '<div class="birthday-star-info">';
                    finalHtml += '<div class="birthday-star-row">';
                    finalHtml += '<div class="birthday-star-label">用户名</div>';
                    finalHtml += '<div class="birthday-star-name">' + userInfo.userName + '</div>';
                    finalHtml += '</div>';
                    finalHtml += '<div class="birthday-star-row">';
                    finalHtml += '<div class="birthday-star-label">身份</div>';
                    finalHtml += '<div class="birthday-star-role">' + userInfo.powerStatus + '</div>';
                    finalHtml += '</div>';
                    finalHtml += '<div class="birthday-star-row">';
                    finalHtml += '<div class="birthday-star-label">祝福数</div>';
                    finalHtml += '<div class="birthday-star-count">' + userInfo.blessCount + '</div>';
                    finalHtml += '</div></div></div>';
                }
                $("#today-star").html(finalHtml);
                for (let userInfo of userInfoList){
                    let id = userInfo.id;
                    $.ajax({
                        type:"get",
                        url:"user/downloadById",
                        data:{
                            id:id
                        },
                        xhrFields: {
                            responseType: 'blob'
                        },
                        success:function (blob){
                            const url = URL.createObjectURL(blob);
                            $("#" + id).attr("src",url);
                        }
                    })
                }
            }
        }
    })
}
//图片下载
function downloadUserFile(select_file) {
    $.ajax({
        url: "user/download",
        type: 'GET',
        xhrFields: {
            responseType: 'blob'
        },
        success: function(blob) {
            // 直接创建临时文件下载（浏览器会生成随机文件名）
            const url = URL.createObjectURL(blob);
            $(select_file).attr("src",url);
        }
    });
}